export const sortBase = (array: number[]) => {
  let maxValue = array[0];
  let minValue = array[0];

  for (let v of array) {
    if (v > maxValue) {
      maxValue = v;
    }
    if (v < minValue) {
      minValue = v;
    }
  }

  const barrels: number[][] = [[], [], [], [], [], [], [], [], [], []];
  const digit = `${maxValue}`.length;

  for (let i = 1; i <= digit; i++) {
    for(let j = 0; j < array.length; j++) {
      const item = array[j];
      const index = Math.floor(item % 10**i / 10**(i-1));
      barrels[index].push(item);
    }

    let k = 0;
    for(let j = 0; j < barrels.length; j++) {
      let barrel = barrels[j];
      while(barrel.length) {
        array[k] = barrel.shift() as number;
        k++;
      }
    }
  }

  return array;
};
